package gold.digger;

import java.util.Stack;

/**
 * Created by fanzhenyu02 on 2020/2/27.
 */
public class LC503 {

    public int[] nextGreaterElements(int[] arr) {
        int[] res = new int[arr.length];
        Stack<Integer> st = new Stack<>();
        for (int i = 2 * arr.length - 1; i >= 0; i--) {
            while (!st.isEmpty() && st.peek() <= arr[i % arr.length]) {
                st.pop();
            }

            res[i % arr.length] = st.isEmpty() ? -1 : st.peek();
            st.push(arr[i % arr.length]);
        }

        return res;
    }


    public static void main(String[] args) {
        LC503 an = new LC503();

        int[] nums2 = {1, 2, 1};
        System.out.println(an.nextGreaterElements(nums2));


    }
}
